package com.mti.app.view
{
	import com.mti.app.ApplicationFacade;
	import com.mti.app.view.mycomponents.ChoiceView;
	import com.mti.epivisite.common.event.EpivisiteEvent;
	
	import flash.events.MouseEvent;
	
	import mx.effects.Move;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ChoiceViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ChoiceViewMediator";
		private var evenement:EpivisiteEvent;
		
		public function ChoiceViewMediator(viewComponent:ChoiceView)
		{
			super(NAME, viewComponent);
		}
		
		protected function get choiceView():ChoiceView
		{
			return viewComponent as ChoiceView;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationFacade.LOADING_CHOICE_COMPLETE];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ApplicationFacade.LOADING_CHOICE_COMPLETE:
					
					evenement = (EpivisiteEvent (ApplicationMediator.serverEvent.getData()));
					
					choiceView.buttonFiche.addEventListener(MouseEvent.CLICK, ficheClick);
					choiceView.buttonPoint.addEventListener(MouseEvent.CLICK, pointClick);
					choiceView.buttonSuppr.addEventListener(MouseEvent.CLICK, supprClick);
					choiceView.buttonMDP.addEventListener(MouseEvent.CLICK, mdpClick);
					choiceView.buttonAnnuler.addEventListener(MouseEvent.CLICK, cancelClickHandler);
					choiceView.buttonSauvegarder.addEventListener(MouseEvent.CLICK, saveClickHandler);
					break;
			}
		}

		
		public function decoClick(event:MouseEvent):void
		{
			sendNotification(ApplicationFacade.VIEW_LOGIN);
		}
		public function supprClick(event:MouseEvent):void
		{
			ApplicationMediator.serverEvent.deleteEvent(evenement);
			sendNotification(ApplicationFacade.VIEW_LOGIN);
		}
		
		public function pointClick(event:MouseEvent):void
		{
			sendNotification(ApplicationFacade.COMMAND_LOAD_QRCODE);
			sendNotification(ApplicationFacade.VIEW_QRCODE);
		}
		public function ficheClick(event:MouseEvent):void
		{
			ApplicationMediator.serverEvent.setData(evenement);
			sendNotification(ApplicationFacade.COMMAND_LOAD_EVENEMENT);
			sendNotification(ApplicationFacade.VIEW_EVENEMENT);
		}
		
		public function mdpClick(event:MouseEvent):void
		{
			choiceView.buttonMDP.enabled = false;
			var move:Move = new Move(choiceView.groupButtons);
			move.yBy = -200;
			var move2:Move = new Move(choiceView.groupMDP);
			move2.yBy = -200;
			move.play();
			move2.play();
		}

		public function saveClickHandler(event:MouseEvent):void
		{
			if (this.choiceView.fieldNewPass.text != "" && this.choiceView.fieldNewPass.text == this.choiceView.fieldConfirmPass.text)
			{
				ApplicationMediator.serverEvent.updatePassword(evenement, this.choiceView.fieldNewPass.text);
				choiceView.fieldConfirmPass.text = "";
				choiceView.fieldNewPass.text = "";
				choiceView.labelError.visible = false;
				var move:Move = new Move(choiceView.groupButtons);
				move.yBy = 200;
				var move2:Move = new Move(choiceView.groupMDP);
				move2.yBy = 200;
				move.play();
				move2.play();
				choiceView.buttonMDP.enabled = true;
			}
			else
			{
				choiceView.labelError.visible = true;
				choiceView.labelError.text = "Erreur, réessayez.";
			}
		}
		
		public function cancelClickHandler(event:MouseEvent):void
		{
			choiceView.fieldConfirmPass.text = "";
			choiceView.fieldNewPass.text = "";
			choiceView.labelError.visible = false;
			var move:Move = new Move(choiceView.groupButtons);
			move.yBy = 200;
			var move2:Move = new Move(choiceView.groupMDP);
			move2.yBy = 200;
			move.play();
			move2.play();
			choiceView.buttonMDP.enabled = true;
		}
	}
}